<?
/*
 *      index.php
 *      
 *      Copyright 2011 Francesco Ricci <francesco@ricci.in>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
 
$db = mysql_connect(localhost, "root", "root") or die ("Errore!");
mysql_select_db("sms", $db);
?>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" media="all">
body {
font-size: 12px;
font-family: Verdana, Arial, Helvetica, sans-serif;
margin: 0px;
padding: 0px;
}
table {
font-size: 12px;
font-family: Verdana, Arial, Helvetica, sans-serif;
margin: 0px;
padding: 0px;
}

</style>
</head>

<table style="width: 100%;"border="0" bordercolor="#888888" cellspacing="0">
<tbody>
<tr>
<td style="width: 25%;">
<center>
<form action="index.php" method=post><input type="submit" value="Ultimi 100"></form><form action="index.php?act=all" method=post><input type="submit" value="Guarda tutti"></form>
</center>
</td>

<td style="width: 25%;">
<center>
Ricerca contatto <form action="index.php?act=contact" method=post>
<select name="phone">
<?
$sql = "SELECT DISTINCT (`to`)  FROM `sms` UNION SELECT DISTINCT (`from`)  FROM `sms`";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
for($x=0;$x<$numrows;$x++)
{
	$resrow = mysql_fetch_row($result);
	echo '<option value="'.$resrow[0].'">'.$resrow[0].'</option>';
	
}


?>
</select>
<input type="submit" value="Cerca!">
</form>
</center>
</td>

<td style="width: 25%;">
<center>
Ricerca tag <form action="index.php?act=tag" method=post>
<select name="id_tag">
<?
$sql = "select id_tag,descr from tag_list";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
for($x=0;$x<$numrows;$x++)
{
	$resrow = mysql_fetch_row($result);
	echo '<option value="'.$resrow[0].'">'.$resrow[1].'</option>';
	
}


?>
</select>
<input type="submit" value="Cerca!">
</form>
</center>
</td>

<td style="width: 25%;">
<center>
Ricerca parola<form action="index.php?act=search" method=post><input name="search" type="text">
<input type="submit" value="Cerca!">
</form>
</center>
</td>
</tr>
</tbody>
</table>
<br>
<?



// Crea tag
$sql = "select * from tag_list";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
$form_tag.='<form action=add_tag.php method=post target=_blank><select name="id_tag">';
for($x=0;$x<$numrows;$x++)
{
	$resrow = mysql_fetch_array($result);
	$form_tag.='<option value="'.$resrow["id_tag"].'">'.$resrow["descr"].'</option>';
	
}
	
	
echo '<table><th align="left">Date</th><th align="left">From</th><th align="left">To</th><th align="left">Message</th><th>Tag</th>';

if (!isset($_GET["act"]))
	$sql = "select * from sms order by id_sms DESC limit 100";
else
	if ($_GET["act"] == "all")
		$sql = "select * from sms order by id_sms DESC";
	else if ($_GET["act"] == "search")
			$sql = 'select * from sms where message LIKE "%'.$_POST["search"].'%" order by id_sms DESC';
			else if ($_GET["act"] == "contact")
				$sql = 'select * from sms where `to`="'.$_POST["phone"].'" OR `from`="'.$_POST["phone"].'" order by id_sms DESC';
				else if ($_GET["act"] == "tag")
					$sql = "select * from sms,tags,tag_list where tags.id_tag=tag_list.id_tag and sms.id_sms=tags.id_sms and tags.id_tag=".$_POST["id_tag"]." order by sms.id_sms DESC";


$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
if ($numrows==0)
{
	//Nothing for now
}
else
{
	for($x=0;$x<$numrows;$x++)
		{
		$resrow = mysql_fetch_array($result);
		
		// Righe alterne
		if($x%2==0)
			echo '<tr bgcolor="#EEEEEE">';
		else
			echo '<tr>';
		
		// Seconda parte form per tag
		$form_tag2='</select><input type="hidden" name="id_sms" value="'.$resrow["id_sms"].'"><input type="submit" value="Tagga!"></form>';
		
		// Seleziona tag
		$sql2 = "select tag_list.descr from tags,tag_list where tags.id_tag=tag_list.id_tag and tags.id_sms=".$resrow["id_sms"];
		$result2 = mysql_query($sql2);
		$numrows2 = mysql_num_rows($result2);
		for($x2=0;$x2<$numrows2;$x2++)
		{
			$resrow2 = mysql_fetch_array($result2);
			$tags.=$resrow2[0]."<br>";
		}
		
		// Inviati - Ricevuti
		if ($resrow["from"]!="Me")
			echo '<td><font color="blue">'.date("d/m/y H:i:s",$resrow["time"]+3600).'</font></td><td><font color="blue">'.$resrow["from"].'</font></td><td><font color="blue">'.$resrow["to"].'</font></td><td><font color="blue">'.$resrow["message"].'</font></td><td>'.$tags."<br>".$form_tag.$form_tag2.'</td></tr>';
		else
			echo '<td><font color="red">'.date("d/m/y H:i:s",$resrow["time"]+3600).'</font></td><td><font color="red">'.$resrow["from"].'</font></td><td><font color="red">'.$resrow["to"].'</font></td><td><font color="red">'.$resrow["message"].'</font></td><td>'.$tags."<br>".$form_tag.$form_tag2.'</td></tr>';
		
		$tags='';
		}
	echo '</table>';
}
?>
